package com.yifi.module.jwflow.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.plugins.JwFlowPlugin;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolDataBase;
import com.yifi.common.utils.ToolString;
import com.yifi.module.jwflow.service.DesignService;
import io.gitee.yaoshanli.engine.model.WfCodeModel;
import io.gitee.yaoshanli.engine.model.WfNodeModel;
import io.gitee.yaoshanli.engine.service.impl.CodeService;
import io.gitee.yaoshanli.engine.service.impl.NodeService;

import java.util.List;

@Path(value="/jwflow",viewPath = "/jwflow/")
public class jwflow_controller extends BaseController {

    @Inject
    DesignService service;

    public void index()
    {
        render("jwflow.html");
    }

    public void list()
    {
        String sqlSelect="select * ";
        String sqlExcept=" from jw_wf_code order by order_num,name";
        renderJson(getDataPage(sqlSelect,sqlExcept));
    }

    public void edit()
    {
        String id=getPara("id","");
        set("id",id);
        render("jwflow_edit.html");
    }

    public void getData()
    {
        String id=getPara("id","");
        String sql="select * from jw_wf_code where id='"+id+"'";

        renderJson(Db.findFirst(sql));
    }

    public void remove()
    {
        String ids=getPara("ids","");
        ids= ToolString.getIdsToStr(ids);
        String sql="delete from jw_wf_code where id in ("+ids+")";
        if(Db.update(sql)>0)
        {
            renderJson(success("成功"));
        }
        else
        {
            renderJson(error("失败"));
        }
    }

    public void design()
    {
        String id=getPara("id","");
        set("id",id);
        render("wf_design.html");
    }

    public void save()
    {
        String id=getPara("id","");
        String code=getPara("code","");
        String name=getPara("name","");
        String desc=getPara("desc","");
        String status=getPara("status","");
        String main_table=getPara("main_table","");
        String main_field=getPara("main_field","");
        String detail_url=getPara("detail_url","");
        String app_url=getPara("app_url","");

        String sql="select * from jw_wf_code where id='"+id+"'";
        Record record = Db.findFirst(sql);
        boolean isedit=true;
        if(record==null)
        {
            isedit=false;
            record = new Record();
            record.set("id", IdUtil.fastUUID());
        }
        record.set("code", code);
        record.set("name", name);
        record.set("desc", desc);
        record.set("status", status);
        record.set("main_table", main_table);
        record.set("main_field", main_field);
        record.set("detail_url", detail_url);
        record.set("app_url", app_url);
        if(DbUtils.save("jw_wf_code","id",record,isedit))
        {
            renderJson(success("成功"));
        }
        else
        {
            renderJson(error("失败"));
        }
    }

    public void getJson()
    {
        String id=getPara("id","");
        String jsonData=service.getJsonData(id);
        renderJson(jsonData);
    }

    public void getJsonView()
    {
        String id=getPara("id","");
        String task_id=getPara("task_id","");

        String jsonData=service.getJsonData(id);
        renderJson(jsonData);
    }

    public void saveDesign()
    {
        String id=getPara("id","");
        String jsonData=getPara("jsonData","");
        String nodesStr=getPara("nodes","");
        String edgesStr=getPara("edges","");

        JSONArray nodes= JSONArray.parseArray(nodesStr);
        JSONArray edges=JSONArray.parseArray(edgesStr);

        service.saveNodes(id,nodes);
        service.saveLinks(id,edges);
        String sql="select * from jw_wf_code where id='"+id+"'";
        Record record = Db.findFirst(sql);
        record.set("json_data", jsonData);
        Db.update("jw_wf_code","id",record);

        renderJson(success("成功"));
    }

    @Inject
    private CodeService codeService;
    @Inject
    private NodeService nodeService;
    public void prop()
    {
        //节点ID
        String node_id=getPara("node_id","");
        set("node_id",node_id);
        //工作流ID
        String wf_id=getPara("wf_id","");
        set("wf_id",wf_id);
        WfCodeModel wfCodeModel= codeService.getWfCode(wf_id);
        set("wf",wfCodeModel);

        WfNodeModel wfNodeModel=nodeService.getNode(node_id);
        set("node",wfNodeModel);

        String sql="select * from jw_wf_group order by order_num";
        List<Record> list=Db.find(sql);
        set("group",list);

        sql="select * from jw_wf_group_filter order by order_num";
        list=Db.find(sql);
        set("filter",list);

        render("wf_design_prop.html");
    }


    public void getProp()
    {
        String id=getPara("id","");
        String sql="select * from jw_wf_node where id=?";
        Record record= Db.findFirst(sql,id);
        if(record==null)
        {
            renderJson(error("出错"));
            return;
        }

        //节点ID
        String node_id=id;  //getPara("node_id","");
        record.set("node_id",node_id);
        //工作流ID
        String wf_id=getPara("wf_id","");
        record.set("wf_id",wf_id);
        WfCodeModel wfCodeModel= codeService.getWfCode(wf_id);
        record.set("wf",wfCodeModel);

        WfNodeModel wfNodeModel=nodeService.getNode(node_id);
        record.set("node",wfNodeModel);

        sql="select * from jw_wf_group order by order_num";
        List<Record> list=Db.find(sql);
        record.set("group",list);

        sql="select * from jw_wf_group_filter order by order_num";
        list=Db.find(sql);
        record.set("filter",list);

        renderJson(record);
    }

    public void saveProp()
    {
        String id=getPara("id","");
        String detail_url=getPara("detail_url","");
        String deal_action=getPara("deal_action","");
        String app_url=getPara("app_url","");
        String deal_back=getPara("deal_back","0");  //发回类型
        String deal_attr=getPara("deal_attr","0");  //节点处理属性
        String deal_type=getPara("deal_type","0");  //节点审批处理规则
        String deal_type_rate=getPara("deal_type_rate","0");    //节点审批处理占比通过
        String by_group=getPara("by_group","0");    //审批人员组
        String is_back=getPara("is_back","0");  //是否可撤回
        String is_filter=getPara("is_filter","0");  //过滤人员设置
        String is_cancel=getPara("is_cancel","0");  //是否作废

        String sql="select * from jw_wf_node where id=?";
        Record record= Db.findFirst(sql,id);
        record.set("detail_url",detail_url);
        record.set("deal_action",deal_action);
        record.set("app_url",app_url);
        record.set("deal_back",deal_back);
        record.set("deal_attr",deal_attr);
        record.set("deal_type",deal_type);
        record.set("deal_type_rate",deal_type_rate);
        record.set("by_group",by_group);
        record.set("is_back",is_back);
        record.set("is_filter",is_filter);
        record.set("is_cancel",is_cancel);
        if(Db.update("jw_wf_node","id",record))
        {
            renderJson(success("成功"));
        }
        else
        {
            renderJson(error("失败"));
        }
    }


    public void condition()
    {
        //节点ID
        String node_id=getPara("node_id","");
        set("node_id",node_id);
        //工作流ID
        String wf_id=getPara("wf_id","");
        set("wf_id",wf_id);

        //当前节点的下一步的几点集合
        List<WfNodeModel> list = JwFlowPlugin.getEngine().flow().getNextNodes(wf_id,node_id);
        set("nodes",list);

        //当前流程信息
        WfCodeModel wfCodeModel = JwFlowPlugin.getEngine().flow().getWfCode(wf_id);
        String main_table=wfCodeModel.getMain_table();
        //根据主表取得主表的结构，去处理配置
        String dbName= ToolDataBase.getDbInfo().dbName;
        String sql = "select c.table_schema,t.table_name ,t.table_comment, c.column_name  as fieldname,c.column_comment  as fielddescript,"
                + "c.ordinal_position,c.column_default, c.is_nullable,c.data_type  as fieldtype,c.character_maximum_length,c.numeric_precision,"
                + " c.numeric_scale,c.column_type ,c.column_key 'key',c.extra"
                + "  from  information_schema.`tables` t "
                + "left join information_schema.`columns` c on t.table_name = c.table_name "
                + "and t.table_schema = c.table_schema " + "where   t.table_schema = '" + dbName
                + "'  and t.table_name='" + main_table + "' order by   c.table_name, c.ordinal_position";
        List<Record> table=Db.find(sql);
        set("table",table);

        render("wf_design_condition.html");
    }

    public void getCondition()
    {

    }

    public void saveCondition()
    {

    }
}
